<?php

namespace App\Http\Controllers\Api;

use App\Services\MaterielClassService;
use App\V3\V3_1\Models\MaterielClassModel;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;
use Illuminate\Http\Request;
use App\Helper\Utils\Util;

class MaterielClassController extends BaseController
{
    /**
     * @物料分类列表
     * created 在 2021/10/20 16:17 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
        ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        return $this->success(
            MaterielClassService::index($this->getMainUserId(), $data)
        );
    }

    /**
     * @物料分类-下拉
     * created 在 2021/10/21 9:55 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function select()
    {
        return $this->success(
            MaterielClassService::select($this->getMainUserId())
        );
    }

    /**
     * @新建物料分类
     * created 在 2021/10/22 15:23 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            //type:0->一级分类;1->二级分类
            'type' => [
                'required',
                Rule::In([0, 1]),
            ],//页面大小
            'class_name' => 'required',//分类名称
            'class_no' => 'required',//分类编号
            'parent_id' => 'required|integer',//上级分类id
        ],
            [
                'type.in' => '类型不合法，请重新选择',
                'class_name.required' => '请输入分类名称',
                'class_no.required' => '请输入分类编号',
            ]);

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $result = MaterielClassService::add($this->getMainUserId(), $this->getUserId(), $data);

        if(!$result['code']) {
            $this->log('新建物料分类', $data);
            return $this->success(['id' => $result['data']]);
        }
        else {
            return $this->fail('', $result['msg']);
        }
    }

    public function del(Request $request)
    {
        $data = $request->except('code');
        return response()->json(
            MaterielClassService::del($data)
        );
    }

    /**
     * @删除指定分类
     * created 在 2021/10/23 09:23 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del1(Request $request)
    {
        $data = $request->all();

        $result = MaterielClassService::del($data);

        if($result['code']) {
            return $this->fail('', $result['msg']);
        }
        else {
            //type:0->一级分类;1->二级分类
            if(!$data['type']) {
                $this->log('删除一级物料分类', $data);
            }
            else {
                $this->log('删除二级物料分类', $data);
            }

            return $this->success();
        }
    }

    /**
     * @修改指定分类
     * created 在 2021/10/23 10:41 创建
     * @author SunWu
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request)
    {
        //参数验证
        $validator = Validator::make($data = $request->all(), [
            'id' => 'required|integer',
            //type:0->一级分类;1->二级分类
            'type' => [
                'required',
                Rule::In([0, 1]),
            ],//页面大小
            'class_name' => 'required',//分类名称
            'class_no' => 'required',//分类编号
            'parent_id' => 'required|integer',//上级分类id
        ],
            [
                'type.in' => '类型不合法，请重新选择',
                'class_name.required' => '请输入分类名称',
                'class_no.required' => '请输入分类编号',
            ]
        );

        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        $result = MaterielClassService::edit($this->getMainUserId(), $data);

        if($result['code']) {
            return $this->fail('', $result['msg']);
        }
        else {
            $this->log('修改分类信息', $data);

            return $this->success();
        }
    }

    //生成分类随机编号
    public function randCode() {
        $data['class_no'] = Util::randStr(4);

        while(!empty($checkClassNo = MaterielClassModel::checkMaterielClassCode($data['class_no'], $this->getMainUserId()))) {
            $data['class_no'] = Util::randStr(4);
        }

        return $this->success($data);
    }
}
